#include "ffrt_inner.h"
#include "ffrt.h"
#include <iostream>

using namespace std;

#include <thread>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#include <string.h>
 
void handle_client(int client_socket) {
    char buffer[1024];
    while (true) {
 	//if (ffrt::this_task::get_id() != 0) {
	 //  ffrt::sync_io(client_socket);
	   cout << "111111111111111222111111111" << endl;
//	}
    fd_set readfds;  
    struct timeval tv;  
    int retval;  
    int nbytes = -1;
    // 初始化文件描述符集合  
    FD_ZERO(&readfds);  
    FD_SET(client_socket, &readfds);  
  
    // 设置超时时间  
    tv.tv_sec = 2;  // 5 秒  
    tv.tv_usec = 0; // 0 微秒  
  
    // 等待数据可读，直到超时  
    retval = select(client_socket + 1, &readfds, NULL, NULL, &tv);  
    if (retval == -1) {  
        std::cerr << "select() failed" << std::endl;  
        return;  
    } else if (retval) {  
        // 数据可读，调用 recv  
        nbytes = recv(client_socket, buffer, sizeof(buffer), 0);  
        if (nbytes == -1) {  
            std::cerr << "recv() failed" << std::endl;  
            return;  
        } else if (nbytes == 0) {  
            std::cout << "Connection closed by peer" << std::endl;  
        } else {  
            std::cout << "Received " << nbytes << " bytes: " << buffer << std::endl;  
        }  
    } else {  
        // 超时  
        std::cout << "Timeout occurred" << std::endl;  
    }  


       // ssize_t bytes_read = recv(client_socket, buffer, sizeof(buffer), 0);
        if (nbytes <= 0) {
            // 客户端断开或者发生错误
            close(client_socket);
            return;
        }
 
        // 对消息进行处理
        std::cout << "Received: " << buffer << std::endl;
 
        // 回复客户端
        std::string reply = "Server received the message.";
        send(client_socket, reply.c_str(), reply.length(), 0);
    }
}
 
int main() {
    int server_socket, client_socket;
    struct sockaddr_in server_address;
 
    // 创建socket
    server_socket = socket(AF_INET, SOCK_STREAM, 0);
 
    // 设置服务器地址
    server_address.sin_family = AF_INET;
    server_address.sin_addr.s_addr = INADDR_ANY;
    server_address.sin_port = htons(9000);
 
    // 绑定socket到地址
    if (bind(server_socket, (struct sockaddr*)&server_address, sizeof(server_address)) < 0) {
        std::cerr << "Bind failed" << std::endl;
        return -1;
    }
 
    // 监听socket
    if (listen(server_socket, 5) < 0) {
        std::cerr << "Listen failed" << std::endl;
        return -1;
    }
 
    std::cout << "Server is listening on port 9000..." << std::endl;
 
    while (true) {
        socklen_t client_address_length = sizeof(server_address);
        client_socket = accept(server_socket, (struct sockaddr*)&server_address, &client_address_length);
        if (client_socket < 0) {
            std::cerr << "Accept failed" << std::endl;
            continue;
        }
 
        std::cout << "New client connected" << std::endl;
 
        // 为每个客户端创建一个线程
       // std::thread client_thread(handle_client, client_socket);
       // client_thread.detach(); // 让线程在后台运行
	std::function<void()> TestClient = [client_socket] () {
	  handle_client(client_socket);
	};
	ffrt::submit(std::move(TestClient), {}, {}, ffrt::task_attr().name("TestClient"));
    }
 
    close(server_socket);
    return 0;
}
